package com.example.nonggo.adpter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.nonggo.R;
import com.example.nonggo.bean.Category;

import java.util.ArrayList;
import java.util.List;

public class MarketTrendsAdapter extends RecyclerView.Adapter<MarketTrendsAdapter.CategoryViewHolder> {

    private List<Category> categories;
    private List<Category> nowCategories;
    private int level = 0;
    private OnChangeListener onChangeListener;

    public MarketTrendsAdapter(OnChangeListener onChangeListener) {
        this.categories = new ArrayList<>();
        this.nowCategories = new ArrayList<>();
        this.onChangeListener = onChangeListener;

    }

    @SuppressLint("NotifyDataSetChanged")
    public void setCategories(List<Category> categories) {
        if (categories == null || categories.isEmpty()) {
            categories = new ArrayList<>();
        }
        this.categories = categories;
        this.nowCategories = categories;
        if (!this.nowCategories.isEmpty()) {
            this.nowCategories.get(0).setSelected(true);
            onChangeListener.onClick(this.nowCategories.get(0));
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public CategoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_category, parent, false);
        CategoryViewHolder holder = new CategoryViewHolder(view);
        view.setOnClickListener(v -> {
            int position = holder.getAdapterPosition();
            if (position != RecyclerView.NO_POSITION) {
                setSelectedItemPosition(position);
            }
        });
        return holder;
    }

    @Override
    @SuppressLint("NotifyDataSetChanged")
    public void onBindViewHolder(@NonNull CategoryViewHolder holder, @SuppressLint("RecyclerView") int position) {
        Category category = nowCategories.get(position);
        holder.textView.setText(category.getLabel());
        holder.textView.setSelected(category.isSelected());
    }

    @Override
    public int getItemCount() {
        return nowCategories.size();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setSelectedItemPosition(int position) {
        for (int index = 0; index < nowCategories.size(); index++) {
            Category category = nowCategories.get(index);
            if (category.isSelected() && index != position) {
                category.setSelected(false);
            }
        }
        Category category = nowCategories.get(position);
        if (category != null) {
            category.setSelected(true);
            onChangeListener.onClick(category);
        }
        notifyDataSetChanged();
    }


    public boolean prevList(Category category) {
        if (category == null) {
            return false;
        }
        if (level > 0) {
            level--;
            level = Math.max(level, 0);
            String parentId = category.getParentId();
            nowCategories = findParents(categories, parentId);
            setSelectedItemPosition(0);
            return true;
        }
        return false;
    }


    private List<Category> findParents(List<Category> categories, String parentId) {
        for (int i = 0; i < categories.size(); i++) {

            if (parentId.startsWith(categories.get(i).getCode())) {
                if (categories.get(i).getCode().equals(parentId)) {
                    return categories;
                } else if (categories.get(i).getChildren() != null) {
                    return findParents(categories.get(i).getChildren(), parentId);
                }
            }
        }
        return categories;
    }

    public boolean next(Category category) {
        if (category == null) {
            return false;
        }
        if (nowCategories == null || nowCategories.isEmpty()) {
            setSelectedItemPosition(0);
            return true;
        }
        if (category.getChildren() != null && !category.getChildren().isEmpty()) {
            level++;
            level = Math.min(level, nowCategories.size() - 1);
            this.nowCategories = category.getChildren();
            setSelectedItemPosition(0);
            return true;
        }

        if (level == 0) {
            this.nowCategories = categories;
            setSelectedItemPosition(0);
            return true;
        }

        return false;
    }

    public static class CategoryViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public CategoryViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.textView);
        }
    }

    public interface OnChangeListener {
        void onClick(Category category);
    }
}

